package 链表;

/**
 * https://leetcode-cn.com/problems/middle-of-the-linked-list/
 * @author 会玩的洋洋
 *
 */

public class _876_链表的中间结点 {
	/**
	 * 快慢指针
	 * @param head 目标节点
	 * @return ListNode
	 */
	public ListNode middleNode(ListNode head) {
		ListNode fast = head , slow = head;
        while(fast != null && fast.next != null){
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
	
	/**
	 * 单指针
	 * @param head 目标节点
	 * @return ListNode
	 */
	public ListNode middleNode2(ListNode head) {
		ListNode node = head;
		int sum = 0;
		while(node != null) {
			node = node.next;
			++sum;
		}
		node = head;
		for(int i = 0 ; i < sum/2 ; ++i) {
			node = node.next;
		}
		return node;
	}
}
